//
//  TGadLoookMapVC.m
//  TongGuanLease
//
//  Created by Henghuitong on 2022/6/18.
//

#import "TGadLoookMapVC.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <MAMapKit/MAMapKit.h>
@interface TGadLoookMapVC ()<MAMapViewDelegate>

@property (nonatomic, strong) MAMapView *mapview;

@end

@implementation TGadLoookMapVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"地图";

    [self setupMapView];
}

- (void)setupNavigationItems
{
    [super setupNavigationItems];
    [self setNavStyleRedAndBackBtn];
    
}


- (void)setupMapView
{
    self.mapview = [[MAMapView alloc] init];
    self.mapview.delegate = self;
    [self.view addSubview: self.mapview];
    
    [self.mapview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_offset(0);
        make.bottom.mas_offset(IS_IPHONEX?-20:0);
    }];
        
}



- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    
    [self addPointAnnotation:self.coordinate];
    
}



//添加标注
-(void)addPointAnnotation:(CLLocationCoordinate2D)currentCoordinate
{
    [self.mapview removeAnnotations:self.mapview.annotations];

    
    MAPointAnnotation *point = [[MAPointAnnotation alloc] init];
    point.coordinate = CLLocationCoordinate2DMake(currentCoordinate.latitude,currentCoordinate.longitude);
//    point.lockedToScreen = YES;
//    point.lockedScreenPoint = CGPointMake(self.mapview.bounds.size.width / 2.0, self.mapview.bounds.size.height / 2.0) ;
//    point.lockedScreenPoint = self.mapview.center;
    NSLog(@"添加标注====%f ==%f",point.coordinate.latitude,point.coordinate.longitude);

    [self.mapview addAnnotation:point];
    //设置地图
    [self.mapview setZoomLevel:18 animated:YES];
    [self.mapview selectAnnotation:point animated:YES];
    [self.mapview setCenterCoordinate:currentCoordinate animated:NO];
    
}


#pragma mark - mapview delegate

/**
 * @brief 地图区域即将改变时会调用此接口
 * @param mapView 地图View
 * @param animated 是否动画
 */
- (void)mapView:(MAMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    NSLog(@"地图区域即将改变时");
   

}

/**
 * @brief 地图将要发生移动时调用此接口
 * @param mapView 地图view
 * @param wasUserAction 标识是否是用户动作
 */
- (void)mapView:(MAMapView *)mapView mapWillMoveByUser:(BOOL)wasUserAction;
{
 
    NSLog(@"地图将要发生移动时");
}
/**
 * @brief 地图移动结束后调用此接口
 * @param mapView 地图view
 * @param wasUserAction 标识是否是用户动作
 */
- (void)mapView:(MAMapView *)mapView mapDidMoveByUser:(BOOL)wasUserAction
{
    
    NSLog(@"地图移动结束后");
   
}

/**
 * @brief 地图缩放结束后调用此接口
 * @param mapView       地图view
 * @param wasUserAction 标识是否是用户动作
 */
- (void)mapView:(MAMapView *)mapView mapDidZoomByUser:(BOOL)wasUserAction
{
    NSLog(@"地图缩放结束后");

   
}

//地图区域改变完成后会调用此接口
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
   
    NSLog(@"地图区域改变完成后");
    
}

- (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views
{

}
/**
 * @brief 根据anntation生成对应的View
 * @param mapView 地图View
 * @param annotation 指定的标注
 * @return 生成的标注View
 */
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{

    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        NSLog(@"MAPointAnnotation");

        static NSString *pointReuseIdentifier = @"pointReuseIdentifier";
        MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIdentifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIdentifier];
            
            annotationView.canShowCallout= NO;       //设置气泡可以弹出，默认为NO
            annotationView.animatesDrop = YES;        //设置标注动画显示，默认为NO
            annotationView.draggable = NO;        //设置标注可以拖动，默认为NO
        }

        return annotationView;
    }
    
    return nil;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
